<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;

class GameEntranceController extends BaseController
{
    /**
     * @api {post} /game/add  新增游戏
     * @apiVersion 0.1.1
     * @apiName 新增游戏
     * @apiGroup 游戏整合管理
     * @apiParam {number} game_id    游戏功能id，必传
     * @apiParam {string} game_name 游戏名称，必传
     * @apiParam {number} user_grade 用户等级可开启，必传
     * @apiParam {string} start_time 开始时间，必传
     * @apiParam {string} end_time 结束时间，必传
     * @apiParam {array} time_slot 时间段，必传
     * @apiParam {number} status  游戏状态：1开启  2关闭，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function add(Request $request)
    {

        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'game_id'=>'required|numeric',
                'game_name'=>'required',
                'status'=>'required|in:1,2',
                'user_grade'=>'required|numeric',
                'start_time'=>'required|date_format:"Y-m-d"',
                'end_time'=>'required|date_format:"Y-m-d"|after_or_equal:start_time',
                'time_slot'=>'required',
            ]
        );
        if($v->fails()) throw new BasicException(10001);
        $param = $request->only(['game_id','game_name','status','user_grade','start_time','end_time','time_slot']);
        app('repo_game_entrance')->limitGameId($param['game_id']);
        $param['add_time'] =time();
        $param['start_time'] =strtotime($param['start_time']);
        $param['end_time'] =strtotime($param['end_time'].' 23:59:59');
        $arr_time_slot =explode(',',$param['time_slot']);
        $new_time_slot =[];
        foreach ($arr_time_slot as $key=>$value){
            $new_time_slot[] =explode('-',$value);
        }
        foreach ($new_time_slot as $key=>$value){
            foreach ($value as $k=>$v){
                if ($v<0 || $v>24){
                    throw new BasicException(50024);
                }
            }
        }
        $param['time_slot'] =json_encode($new_time_slot);
        app('repo_game_entrance')->add($param);
        return Response::sendData(true);
    }

    /**
     * @api {post} /game/detail  游戏详情
     * @apiVersion 0.1.1
     * @apiName 游戏详情
     * @apiGroup 游戏整合管理
     * @apiParam {number} id    游戏id，必传
     * @apiSuccessExample 成功响应
         {
            "code": 200,
            "data": {
                "id(主键id)": 2,
                "game_id（功能id）": 111111,
                "game_name（游戏名称）": "测试游戏",
                "user_grade（等级）": 2,
                "start_time（开始时间）": "2020-09-24",
                "end_time（结束时间）": "2020-09-29",
                "status（状态：1开始  2关闭）": 1,
                "time_slot（时间段）": [
                    [
                    "1,3"
                    ],
                    [
                    "8,24"
                    ]
                ],
                "add_time（添加时间）": 1600929360
            }
        }
     */
    public  function  detail(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'=>'required|numeric',
            ]
        );
        if($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        $detail =  app('repo_game_entrance')->detail($id);
        $detail['start_time'] =date('Y-m-d',$detail['start_time']);
        $detail['end_time'] =date('Y-m-d',$detail['end_time']);
        $detail['time_slot'] =json_decode($detail['time_slot'],true);
        return Response::sendData($detail);
    }

    /**
     * @api {post} /game/save  编辑游戏
     * @apiVersion 0.1.1
     * @apiName 编辑游戏
     * @apiGroup 游戏整合管理
     * @apiParam {number} id    游戏id，必传
     * @apiParam {string} game_name 游戏名称，必传
     * @apiParam {number} user_grade 用户等级可开启，必传
     * @apiParam {string} start_time 开始时间，必传
     * @apiParam {string} end_time 结束时间，必传
     * @apiParam {array} time_slot 时间段，必传
     * @apiParam {number} status  游戏状态：1开启  2关闭，必传
     * @apiSuccessExample 成功响应
     *   *{"code":200,"data":true}
     */
    public function save(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'=>'required|numeric',
                'game_id'=>'required|numeric',
                'game_name'=>'required',
                'status'=>'required|in:1,2',
                'user_grade'=>'required|numeric',
                'start_time'=>'required|date_format:"Y-m-d"',
                'end_time'=>'required|date_format:"Y-m-d"|after_or_equal:start_time',
                'time_slot'=>'required',
            ]
        );
        if($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param = $request->only(['id','game_id','game_name','status','user_grade','start_time','end_time','time_slot']);
        $param['start_time'] =strtotime($param['start_time']);
        $param['end_time'] =strtotime($param['end_time'].' 23:59:59');
        $arr_time_slot =explode(',',$param['time_slot']);
        $new_time_slot =[];
        foreach ($arr_time_slot as $key=>$value){
            $new_time_slot[] =explode('-',$value);
        }
        foreach ($new_time_slot as $key=>$value){
            foreach ($value as $k=>$v){
                if ($v<0 || $v>24){
                    throw new BasicException(50024);
                }
            }
        }
        $param['time_slot'] =json_encode($new_time_slot);
        app('repo_game_entrance')->save($param);
        return Response::sendData(true);
    }

    /**
     * @api {post} /game/del  删除游戏
     * @apiVersion 0.1.1
     * @apiName 删除游戏
     * @apiGroup 游戏整合管理
     * @apiParam {number} id    游戏id，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public  function  del(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'=>'required|numeric',
            ]
        );
        if($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        app('repo_game_entrance')->del($id);
        return Response::sendData(true);
    }



    /**
     * @api {post} /game/list  游戏列表
     * @apiVersion 0.1.1
     * @apiName 游戏列表
     * @apiGroup 游戏整合管理
     * @apiSuccessExample 成功响应
    {
        "code": 200,
        "data": [
            {
            "id（主键）": 1,
            "game_id（功能id）": 10002,
            "game_name（游戏名称）": "测试游戏",
            "user_grade（等级）": 2,
            "start_time（开始时间）": "2020-09-25 09:06:58",
            "end_time（结束时间）": "2020-09-25 09:06:58",
            "status（状态：1 开启  2关闭）": 1,
            "time_slot（时间段）": "[[8,15],[16,20]]",
            "add_time（添加时间）": "2020-09-25 09:06:58"
            }
        ]
    }
     */
    public  function  list(Request $request){
        $list =  app('repo_game_entrance')->list();
        if ($list){
          foreach ($list as  $key=>&$value){
              $value['add_time']=date('Y-m-d H:i:s', $value['add_time']);
              $value['start_time']=date('Y-m-d H:i:s', $value['start_time']);
              $value['end_time']=date('Y-m-d H:i:s', $value['end_time']);
          }
        }
        return Response::sendData($list);
    }

}
